package com.weimei.conversionTreasure.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.ComputerBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class TJDVM extends BaseViewModel<Repository> {
    public TJDVM(@NonNull Application application, Repository model) {
        super(application, model);
    }


    public ObservableField<String> alcohol = new ObservableField<>("");
    public ObservableField<String>  temperature = new ObservableField<>("");

    public ObservableField<String>  result = new ObservableField<>("");

    public BindingCommand js = new BindingCommand(() -> {

        if (UserUtils.getFreeNum() > 0){
            jsData();
        }else {
            if (!UserUtils.isVip()){
                ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
                startActivity(VipActivity.class);
                return;
            }

            jsData();
        }


    });

    private void jsData() {

        if (ValidateUtils.isEmptyString(alcohol.get())){
            ToastUtils.showShort("请输入酒度");
            return;
        }

        if (ValidateUtils.isEmptyString(temperature.get())){
            ToastUtils.showShort("请输入温度");
            return;
        }

        Map<String,String> map = new HashMap<>();
        map.put("alcohol",alcohol.get());
        map.put("temperature",temperature.get());
        model.getwine(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<ComputerBean>() {
                    @Override
                    public void onNext(ComputerBean baseResult) {
                        if (baseResult.issucc){
                            UserUtils.opreFree();
                            result.set(baseResult.getData()+"");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
